package com.usooft.metaTalent.admin.system.form;

import com.usooft.framework.core.query.Sort;
import com.usooft.framework.web.form.ListForm;
import com.usooft.metaTalent.core.system.query.RoleQuery;
import lombok.Setter;

@Setter
public class RoleListForm extends ListForm<RoleQuery> {

    private String nameIs;

    private RoleQuery.Filter buildFilter() {
        return RoleQuery.Filter.builder()
                .nameIs(nameIs)
                .build();
    }

    private RoleQuery.Sorts buildSorts() {
        RoleQuery.Sorts.Builder builder = RoleQuery.Sorts.builder();
        for (String sortItem : getSortItems()) {
            Sort.Order order = getSortOrder(sortItem);
            switch (sortItem) {
                case "-createdDate", "createdDate" -> builder.id(order);
            }
        }
        return builder.build();
    }

    public RoleQuery buildQuery() {
        return RoleQuery.builder()
                .filter(buildFilter())
                .sorts(buildSorts())
                .limit(getLimit())
                .build();
    }
}
